﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InitController.cs" company="">
//   Copyright (c) 2010/2011 All Right Reserved
// </copyright>
// <summary>
//   Contains InitController class declaration.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IBupro.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    using iBuproModel;
    using iBuproModel.Providers;
    using iBuproModel.Repositories.Classes;

    /// <summary>
    /// This controller contains action for initializing the application.
    /// </summary>
    public class InitController : Controller
    {
        
        public ActionResult Index()
        {
            var context = new Entities();
            try
            {
                UserRepository userRepository = new UserRepository();

                IBuproRoleProvider roleProvider = new IBuproRoleProvider();

                if (!roleProvider.RoleExists("Admin"))
                    roleProvider.CreateRole("Admin");

                if (!roleProvider.RoleExists("Student"))
                    roleProvider.CreateRole("Student");

                if (!roleProvider.RoleExists("Principal"))
                    roleProvider.CreateRole("Principal");

                if (!roleProvider.RoleExists("DepartmentAdmin"))
                    roleProvider.CreateRole("DepartmentAdmin");

                if (!roleProvider.RoleExists("Lector"))
                    roleProvider.CreateRole("Lector");

                IBuproMembershipProvider memberProvider = new IBuproMembershipProvider();
            
                if (!memberProvider.UserExists("student"))
                {
                    System.Web.Security.MembershipCreateStatus stat;
                    memberProvider.CreateUser("student","student","email@email.com","student","student", out stat);
                }

                if (!memberProvider.UserExists("admin"))
                {
                    System.Web.Security.MembershipCreateStatus stat;
                    memberProvider.CreateUser("admin","admin","email@email2.com","admin","admin", out stat);
                }

                if (!memberProvider.UserExists("student2"))
                {
                    System.Web.Security.MembershipCreateStatus stat;
                    memberProvider.CreateUser("student2", "student2", "email@emagdfil2.com", "student2", "student2", out stat);
                }

                if (!memberProvider.UserExists("lector"))
                {
                    System.Web.Security.MembershipCreateStatus stat;
                    memberProvider.CreateUser("lector", "lector", "lector@lector.com", "lector", "lector", out stat);
                }

                if (!memberProvider.UserExists("principal"))
                {
                    System.Web.Security.MembershipCreateStatus stat;
                    memberProvider.CreateUser("principal", "principal", "principal@principal.com", "principal", "principal", out stat);
                }

                Role adminRole = context.Roles.Where(r => r.Name == "Admin").SingleOrDefault();
                Role studentRole = context.Roles.Where(r => r.Name == "Student").SingleOrDefault();
                Role lectorRole = context.Roles.Where(r => r.Name == "Lector").SingleOrDefault();
                Role principalRole = context.Roles.Where(r => r.Name == "Principal").SingleOrDefault();

                iBuproModel.User student = context.Users.Where(u => u.Username == "student").SingleOrDefault();

                iBuproModel.User student2 = context.Users.Where(u => u.Username == "student2").SingleOrDefault();

                iBuproModel.User admin = context.Users.Where(u => u.Username == "admin").SingleOrDefault();
                if (!roleProvider.IsUserInRole("admin", "Admin"))
                {
                    context.AttachTo("Roles", adminRole);
                    adminRole.Users.Add(admin);
                }

                iBuproModel.User lector = context.Users.Where(u => u.Username == "lector").SingleOrDefault();
                if (!roleProvider.IsUserInRole("lector", "Lector"))
                {
                    
                    context.AttachTo("Roles", lectorRole);
                    lectorRole.Users.Add(lector);
                }

                iBuproModel.User principal = context.Users.Where(u => u.Username == "principal").SingleOrDefault();
                if (!roleProvider.IsUserInRole("principal", "Principal"))
                {
                    
                    context.AttachTo("Roles", principalRole);
                    principalRole.Users.Add(principal);
                }
                
                Department k336 = new Department(){
                    Name = "Katedra počítačů",
                    Shortcut = "K336"
                };


                if (!k336.DepartmentPrincipal.Contains(principal))
                {
                    k336.DepartmentPrincipal.Add(principal);

                    context.AddToDepartments(k336);

                    Subject SI2 = new Subject()
                    {
                        Name = "software engineering 1",
                        Shortcut = "SI2"
                    };

                    Subject SI3 = new Subject()
                    {
                        Name = "software engineering 2",
                        Shortcut = "SI3"
                    };

                    k336.Subjects.Add(SI2);
                    k336.Subjects.Add(SI3);

                    Subject TIN = new Subject();

                    TIN.Name = "Teoretická informatika";
                    TIN.Shortcut = "TIN";

                    lector.LectorSubjects.Add(TIN);
                    k336.Subjects.Add(TIN);
                }

                
                DiplomProjectType bachelorType = new DiplomProjectType();
                bachelorType.Name = "Bachelor Project";
                DiplomProjectType magisterType = new DiplomProjectType();
                magisterType.Name = "Magister Project";

                context.DiplomProjectTypes.AddObject(bachelorType);
                context.DiplomProjectTypes.AddObject(magisterType);

                if (context.DiplomProjectStates.Count() == 0)
                {
                    DiplomProjectState newState = new DiplomProjectState()
                    {
                        Name = "New Project"
                    };

                    DiplomProjectState bookedState = new DiplomProjectState()
                    {
                        Name = "Booked"
                    };

                    DiplomProjectState acceptedState = new DiplomProjectState()
                    {
                        Name = "Accepted by lector"
                    };

                    DiplomProjectState subscribedState = new DiplomProjectState()
                    {
                        Name = "Subscribed"
                    };

                    DiplomProjectState canceledState = new DiplomProjectState()
                    {
                        Name = "Canceled"
                    };

                    DiplomProjectState defendedState = new DiplomProjectState()
                    {
                        Name = "Defended"
                    };

                    context.DiplomProjectStates.AddObject(newState);
                    context.DiplomProjectStates.AddObject(bookedState);
                    context.DiplomProjectStates.AddObject(acceptedState);
                    context.DiplomProjectStates.AddObject(subscribedState);
                    context.DiplomProjectStates.AddObject(canceledState);
                    context.DiplomProjectStates.AddObject(defendedState);
                }


                context.SaveChanges();
            }
            catch
            {
                throw new Exception("Cannot init.");
            }

            return View();
        }
        
    }
}
